package com.grapes.view;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

import com.grapes.R;
import com.grapes.business.ProcedureBusiness;

public class ProcedureManagementActivity extends GrapesActivity {
	public static final int MSG_LOAD_STORED_OK = 4;
	public static final int MSG_LOAD_STORED_FAIL = 5;
	public static final int MSG_LOAD_APPLICATED_OK = 6;
	public static final int MSG_LOAD_APPLICATED_FAIL = 7;
	public static final int MSG_LOAD_REFUSED_OK = 8;
	public static final int MSG_LOAD_REFUSED_FAIL = 9;

	private ProcedureListView storedProcedures;
	private ProcedureListView applicatedProcedures;
	private ProcedureListView refusedProcedures;
	private GestureDetector gestureDetector;
	private ViewFlipper viewFlipper;
	private int selectedListViewIndex = 0;

	private Handler hander = new Handler() {

		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			switch(msg.what)
			{
			case MSG_LOAD_STORED_OK:
			case MSG_LOAD_STORED_FAIL:
				dismissPorgressDialog();
				break;
			}
		}
	};

	public Handler getHandler() {
		return hander;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.procedure_manangement);
		storedProcedures = (ProcedureListView) findViewById(R.id.procedure_stored_list);
		storedProcedures.setProcedureType(ProcedureBusiness.PROCEDURE_STORED);
		applicatedProcedures = (ProcedureListView) findViewById(R.id.procedure_applicated_list);
		applicatedProcedures.setProcedureType(ProcedureBusiness.PROCEDURE_APPLICATED);
		refusedProcedures = (ProcedureListView) findViewById(R.id.procedure_refused_list);
		refusedProcedures.setProcedureType(ProcedureBusiness.PROCEDURE_REFUSED);
		viewFlipper = (ViewFlipper) findViewById(R.id.procedure_flipper);
		gestureDetector = new GestureDetector(ProcedureManagementActivity.this,
				new FlingGestureDetector());
		showProgressDialog();
		storedProcedures.loadData();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		gestureDetector.onTouchEvent(ev);
		return super.dispatchTouchEvent(ev);
	}

	private void switchListView(int targetIndex, boolean moveRightward) {
		while (selectedListViewIndex != targetIndex) {
			selectedListViewIndex++;
			selectedListViewIndex = selectedListViewIndex
					% viewFlipper.getChildCount();
			if (moveRightward) {
				Animation SlideIn = AnimationUtils.loadAnimation(
						ProcedureManagementActivity.this, R.anim.push_right_in);
				Animation slideOut = AnimationUtils
						.loadAnimation(ProcedureManagementActivity.this,
								R.anim.push_right_out);
				viewFlipper.setInAnimation(SlideIn);
				viewFlipper.setOutAnimation(slideOut);
				viewFlipper.showNext();
			} else {
				Animation SlideIn = AnimationUtils.loadAnimation(
						ProcedureManagementActivity.this, R.anim.push_left_in);
				Animation slideOut = AnimationUtils.loadAnimation(
						ProcedureManagementActivity.this, R.anim.push_left_out);
				viewFlipper.setInAnimation(SlideIn);
				viewFlipper.setOutAnimation(slideOut);
				viewFlipper.showPrevious();
			}
		}
	}

	
	class FlingGestureDetector extends GestureDetector.SimpleOnGestureListener {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			float lastX = e2.getX();
			float lastY = e2.getY();
			float previousX = e1.getX();
			float previousY = e1.getY();
			int[] flipperLocationInScreen = new int[2];
			viewFlipper.getLocationOnScreen(flipperLocationInScreen);
			float topThreshold = flipperLocationInScreen[1];
			float bottomThreshold = topThreshold + viewFlipper.getHeight();
			if (lastY < topThreshold && previousY < topThreshold)
				return super.onFling(e1, e2, velocityX, velocityY);
			if (lastY > bottomThreshold && previousY > bottomThreshold)
				return super.onFling(e1, e2, velocityX, velocityY);
			if (Math.abs(lastX - previousX) <= MIN_HORIZONTAL_FlING_DISTANCE)
				return super.onFling(e1, e2, velocityX, velocityY);
			if (velocityX < MIN_HORIZONTAL_FLIGN_VALOCITY)
				return super.onFling(e1, e2, velocityX, velocityY);
			if (lastX < previousX) {
				int targetIndex = Math.abs(selectedListViewIndex - 1)
						% viewFlipper.getChildCount();
				switchListView(targetIndex, false);
			} else {
				int targetIndex = Math.abs(selectedListViewIndex + 1)
						% viewFlipper.getChildCount();
				switchListView(targetIndex, true);
			}
			return true;
		}
	}
}
